package com.qiaofei.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qiaofei.mapper.RentMapper;
import com.qiaofei.pojo.Rent;
import com.qiaofei.pojo.PageBean;
import com.qiaofei.service.RentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class RentServiceImpl extends BaseServiceImpl implements RentService {

    @Autowired
    private RentMapper rentMapper;

    @Override
    public PageBean page(Integer page, Integer pageSize, Map<String ,Object> params, LocalDate begin, LocalDate end) {
        //1. 设置分页参数
        PageHelper.startPage(page,pageSize);
        //2. 执行查询
        List<Rent> rentList = rentMapper.list(params, begin, end);
        Page<Rent> p = (Page<Rent>) rentList;

        //3. 封装PageBean对象
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult(), p.getPages());
        return pageBean;
    }

    @Override
    public int add(Rent rent) {
        rent.setCreateTime(LocalDateTime.now());
        return rentMapper.insert(rent);
    }

    @Override
    public int update(Rent rent) {
        rent.setUpdateTime(LocalDateTime.now());
        return rentMapper.update(rent);
    }

    @Override
    public List<Rent> getByIds(List<Integer> ids) {
        List<Rent> rentList = rentMapper.getByIds(ids);
        return rentList;
    }

    @Override
    public int delete(List<Integer> ids) {
        List<Rent> rentList = getByIds(ids);
        List<String> headerList = new ArrayList<>();
        if(rentList.size() > 0){
            for (Rent rent :
                    rentList) {
                if(rent.getHouseImages().equals("") == false) {
                    headerList.add(rent.getHouseImages());
                }
            }
            deleteImages(headerList);
        }
        return rentMapper.delete(ids);
    }
}
